/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.peritua;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import modeloa.Peritu;
import dBasea.Abokatua;
import dBasea.Peritua;

public class PerituKudeaketa extends Panela {

	private static final long serialVersionUID = -1578746537866997658L;
	private JComponent zenbakia;
	private JTextField zenb;
	private JButton aurkitu;
	private JLabel izena;
	private JTextField ize;
	private JLabel abizenak;
	private JTextField abi;
	private JLabel telefonoa;
	private JFormattedTextField tele;
	private JLabel lblHiria;
	private JComboBox hiri;
	private JButton ok;
	private JButton ezeztatu;
	private Abokatua abok = new Abokatua();
	private Peritua perit = new Peritua();

	public PerituKudeaketa(String tituloa,
			final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout(0, 0));

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);

		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(8, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Peritu NAN:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		zenb = new JTextField(20);
		zenb.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (zenb.getText().length() >= 10) {
					arg0.consume();
				}
			}
		});
		aurkitu = new JButton("Bilatu");
		aurkitu.setIcon(new ImageIcon(PerituKudeaketa.class
				.getResource("/Argazkia/lupita.png")));
		arri.add(zenbakia);
		arri.add(zenb);
		arri.add(aurkitu);
		erdikoa.add(arri);

		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(9);
		arrib.setLayout(fl_arrib);
		izena = new JLabel("Izena:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		ize = new JTextField(20);
		arrib.add(izena);
		arrib.add(ize);
		erdikoa.add(arrib);

		JPanel erdi = new JPanel();
		erdi.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdi = new FlowLayout();
		fl_erdi.setHgap(15);
		fl_erdi.setVgap(8);
		erdi.setLayout(fl_erdi);
		abizenak = new JLabel("Abizenak:");
		abizenak.setFont(new Font("Arial", Font.PLAIN, 14));
		abi = new JTextField(20);
		erdi.add(abizenak);
		erdi.add(abi);
		erdikoa.add(erdi);

		JPanel bost = new JPanel();
		bost.setBackground(SystemColor.activeCaption);
		FlowLayout fl_bost = new FlowLayout();
		fl_bost.setHgap(15);
		fl_bost.setVgap(8);
		bost.setLayout(fl_bost);
		telefonoa = new JLabel("Telefonoa:");
		telefonoa.setFont(new Font("Arial", Font.PLAIN, 14));
		telefonoa.setVerticalAlignment(SwingConstants.BOTTOM);
		try {
			tele = new JFormattedTextField(new MaskFormatter("#########"));
			tele.setColumns(12);
		} catch (ParseException e1) {

			e1.printStackTrace();
		}
		bost.add(telefonoa);
		bost.add(tele);
		erdikoa.add(bost);

		JPanel h = new JPanel();
		h.setBackground(SystemColor.activeCaption);

		lblHiria = new JLabel("Hiria");
		lblHiria.setFont(new Font("Arial", Font.PLAIN, 14));
		h.add(lblHiria);
		hiri = new JComboBox();
		h.add(hiri);

		try {
			boxCarga();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik hiri zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);

			e.printStackTrace();
		}

		erdikoa.add(h);

		JPanel gs = new JPanel();
		gs.setBackground(SystemColor.activeCaption);
		erdikoa.add(gs);
		FlowLayout fl_behekoa = new FlowLayout();
		fl_behekoa.setVgap(20);
		fl_behekoa.setHgap(110);
		JPanel behekoa = new JPanel(fl_behekoa);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Aldaketak ezarri");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);

		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		aurkitu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String nan = zenb.getText();
				if (nan.isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Sartu perituaren NAN-a, mesedez!", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						if (perit.existitzenDaPeritua(nan)) {

							Peritu abokatu = perit.getPerituDatuak(nan);
							if (abokatu != null) {
								ezarri(true);

								ize.setText(abokatu.getIdPeritua());
								abi.setText(abokatu.getAbizenak());
								tele.setText(String.valueOf(abokatu
										.getTelefonoa()));
								hiri.setSelectedItem(abokatu.getHiria());
							}
						} else {
							JOptionPane.showMessageDialog(null,
									"Ez da existitzen sartu duzuna", "Oker",
									JOptionPane.ERROR_MESSAGE);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!perit.existitzenDaPeritua(zenb.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da peritu hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						try {
							Object hir = hiri.getSelectedItem();
							String hu = hir.toString();
							int tfnoa = Integer.parseInt(tele.getText());
							perit.perituAldatu(zenb.getText(), ize.getText(),
									abi.getText(), tfnoa, hu);
							JOptionPane.showMessageDialog(null,
									"Ondo aldatu da informazioa", "",
									JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu telefonoko eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void boxCarga() throws SQLException {
		List<String> lista = abok.getHiriak();
		hiri.removeAllItems();
		if (lista != null) {
			for (String hiria : lista) {
				hiri.addItem(hiria);
			}
		}
	}

	private boolean zerbaitIdatzi() {
		boolean aurki = false;
		aurki = zenb.getText().length() > 0 && ize.getText().length() > 0
				&& abi.getText().length() > 0 && tele.getText().length() > 0;
		return aurki;
	}

	public void eremuakGarbitu() {
		zenb.requestFocus();
		zenb.setEnabled(true);
		zenb.setText("");
		ize.setText("");
		abi.setText("");
		tele.setText("");
		ezarri(false);
		hiri.setSelectedIndex(0);
	}

	private void ezarri(boolean balioa) {
		ize.setEnabled(balioa);
		abi.setEnabled(balioa);
		tele.setEnabled(balioa);
		if (balioa) {

			zenb.setEnabled(false);
		}

		ok.setEnabled(balioa);
	}

}
